import worker from '@ohos.worker';

const parentPort = worker.parentPort


const api = {
    print(payload) {
        console.log(payload.msg);
        return { msg: 'msg has been print.' };
    },
    async asyncCalc(payload) {
        const result = await new Promise((resolve) => setTimeout(() => resolve(payload.params * 2), 1000));
        console.log('jiangtao -> asyncCalc')
        return { msg: `the caculated answer is ${result}.` };
    },
};

parentPort.onmessage = async function (e) {
    const { id, actionType, payload } = e.data;
    const result = await api[actionType].call(this, payload);
    parentPort.postMessage({ id, response: result });
}